﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace HYRElectric.PowerSystem
{
	internal class PanelSystemDetectCommand
	{
		public static string GetImageByType(Document doc, Element element)
		{
			string a = element.Category.Id.IntegerValue.ToString();
			if (a == "OST_LightingFixtures")
			{
				return "Light.png";
			}
			if (a == "OST_LightingDevices")
			{
				return "Switch.png";
			}
			if (!(a == "OST_ElectricalFixtures"))
			{
				if (a == "OST_ElectricalEquipment")
				{
					return "Panel.png";
				}
				if (!(a == "OST_MechanicalEquipment"))
				{
					return "Panel.png";
				}
				return "Machinery.png";
			}
			else
			{
				if ((doc.GetElement(element.GetTypeId()) as FamilySymbol).Family.get_Parameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == 19)
				{
					return "Switch.png";
				}
				return "Socket.png";
			}
		}

		public static List<ElementId> GetAllInvalidEquipment(Document doc, ElementId viewId)
		{
			List<ElementId> list = new List<ElementId>();
			foreach (Element element in PanelSystemDetectCommand.GetElecEquips(doc, viewId))
			{
				if (!(element.Category.Id.IntegerValue.ToString() == "OST_ElectricalEquipment"))
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					List<Connector> list2 = new List<Connector>();
					try
					{
						ConnectorSetIterator connectorSetIterator = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
						while (connectorSetIterator.MoveNext())
						{
							object obj = connectorSetIterator.Current;
							Connector connector = obj as Connector;
							if (connector.Domain == (Autodesk.Revit.DB.Domain)2)
							{
								list2.Add(connector);
							}
						}
					}
					catch (Exception)
					{
						continue;
					}
					if (list2.Count != 0)
					{
						bool flag = false;
						foreach (Connector connector2 in list2)
						{
							ConnectorSet allRefs = connector2.AllRefs;
							if (allRefs != null)
							{
								IEnumerator enumerator3 = allRefs.GetEnumerator();
								{
									while (enumerator3.MoveNext())
									{
										if (((Connector)enumerator3.Current).Owner.Category.Id.IntegerValue.ToString() == "OST_Wire")
										{
											flag = true;
											break;
										}
									}
								}
								if (flag)
								{
									break;
								}
							}
						}
						if (!flag)
						{
							list.Add(familyInstance.Id);
						}
					}
				}
			}
			return list;
		}

		public static void GetAllInvalidPanel(List<ElementId> equSet, Document doc, ElementId viewId)
		{
			if (Common.GetAllPanel(doc, viewId).Count > 0)
			{
				foreach (Element element in Common.GetAllPanel(doc, viewId))
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					if (familyInstance.MEPModel != null && familyInstance.MEPModel.ConnectorManager != null && familyInstance.MEPModel.ConnectorManager.Connectors.Size >= 1)
					{
						bool flag = false;
						foreach (object obj in familyInstance.MEPModel.ConnectorManager.Connectors)
						{
							ConnectorSet allRefs = ((Connector)obj).AllRefs;
							if (allRefs != null)
							{
								IEnumerator enumerator3 = allRefs.GetEnumerator();
								{
									while (enumerator3.MoveNext())
									{
										if (((Connector)enumerator3.Current).Owner.Category.Id.IntegerValue.ToString() == "OST_Wire")
										{
											flag = true;
											break;
										}
									}
								}
								if (flag)
								{
									break;
								}
							}
						}
						if (!flag)
						{
							equSet.Add(familyInstance.Id);
						}
					}
				}
			}
		}

		public static List<ElementId> GetAllElecEquips(Document doc, ElementId viewId)
		{
			List<ElementId> list = new List<ElementId>();
			foreach (Element element in PanelSystemDetectCommand.GetElecEquips(doc, viewId))
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				List<Connector> list2 = new List<Connector>();
				try
				{
					ConnectorSetIterator connectorSetIterator = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
					while (connectorSetIterator.MoveNext())
					{
						object obj = connectorSetIterator.Current;
						Connector connector = obj as Connector;
						if (connector.Domain == (Autodesk.Revit.DB.Domain)2)
						{
							list2.Add(connector);
						}
					}
				}
				catch (Exception)
				{
					continue;
				}
				if (list2.Count != 0)
				{
					list.Add(familyInstance.Id);
				}
			}
			return list;
		}

		public static List<Element> GetDocElecEquips(Document doc, ElementId idView)
		{
			List<Element> list = new List<Element>();
			foreach (Element element in PanelSystemDetectCommand.GetElecEquips(doc, idView))
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				List<Connector> list2 = new List<Connector>();
				try
				{
					ConnectorSetIterator connectorSetIterator = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
					while (connectorSetIterator.MoveNext())
					{
						object obj = connectorSetIterator.Current;
						Connector connector = obj as Connector;
						if (connector.Domain == (Autodesk.Revit.DB.Domain)2)
						{
							list2.Add(connector);
							break;
						}
					}
				}
				catch (Exception)
				{
					continue;
				}
				if (list2.Count != 0)
				{
					list.Add(familyInstance);
				}
			}
			return list;
		}

		public static List<Element> GetElecEquips(Document doc, ElementId viewId)
		{
			List<Element> list = new List<Element>();
			foreach (FamilyInstance familyInstance in ((viewId != ElementId.InvalidElementId) ? new FilteredElementCollector(doc, viewId) : new FilteredElementCollector(doc)).OfClass(typeof(FamilyInstance)).ToElements().Cast<FamilyInstance>().ToList<FamilyInstance>())
			{
				if (familyInstance.MEPModel != null && familyInstance.MEPModel.ConnectorManager != null && familyInstance.MEPModel.ConnectorManager.Connectors.Size >= 1)
				{
					list.Add(familyInstance);
				}
			}
			return list;
		}

		public static DlgPanelSystemDetectByActiveView dlgViewDetect;

		public static DlgPanelSystemDetect dlgDetect;
	}
}
